package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.dao.OrderSettingListDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingListService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingListService.class)
@Transactional
public class OrderSettingListServiceImpl implements OrderSettingListService {
    @Autowired
    private OrderSettingListDao orderSettingListDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 预约列表分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);//分页助手
        Page<Order> page=orderSettingListDao.findByCondition(queryString);
        long total = page.getTotal();//总数据数
        List<Order> orderList = page.getResult();//分页查询的记录集合
//        List<Map> orderSettingList=new ArrayList<Map>();//预约列表集合
//        for (Order order : orderList) {
//            Map<String,Object> orderSetting=new HashMap<>();//预约数据
//            Member m = memberDao.findById(order.getMemberId());
//            orderSetting.put("id",order.getId());
//            orderSetting.put("orderDate",order.getOrderDate());
//            orderSetting.put("orderType",order.getOrderType());
//            orderSetting.put("orderStatus",order.getOrderStatus());
//            orderSetting.put("fileNumber",m.getFileNumber());
//            orderSetting.put("name",m.getName());
//            orderSetting.put("phoneNumber",m.getPhoneNumber());
//            orderSettingList.add(orderSetting);
//        }

        return new PageResult(total,orderList);
    }

    /**
     * 确定预约
     * @param map
     */
    @Override
    public void update(Map map) {
        orderSettingListDao.update(map);
    }

    /**
     * 批量取消预约
     * @param ids
     * @param list
     * @return
     */
    @Override
    public void delete(Integer[] ids, List<Map> list) {
        for (Integer id : ids) {
            orderSettingListDao.deleteById(id);

        }
        for (Map map : list) {
            String orderDate = (String) map.get("orderDate");//预约时间
            try {
                //查询预约设置
                OrderSetting orderSetting= orderSettingDao.findByOrderDate(DateUtils.parseString2Date(orderDate));
                orderSetting.setReservations(orderSetting.getReservations() - 1);//设置已预约人数-1
                orderSettingDao.editReservationsByOrderDate(orderSetting);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 查询取消预约用户的手机
     * @param ids
     * @return
     */
    @Override
    public List<Order> findMemberConditions(Integer[] ids) {
        List<Order> orderList=new ArrayList<>();//取消预约用户的手机号集合
        for (Integer id : ids) {
            Order order=orderSettingListDao.findMemberConditions(id);
            orderList.add(order);
        }
        return orderList;
    }
}
